﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using SyncConfig.Controls;
using AppCommon;
using AppCommon.Controls;
using System.Data.SqlClient;
using SyncLib;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;

namespace SyncConfig
{
    public partial class FrmMain : Form
    {
        public static FrmMain MainForm { get; set; }
        public AdvTreeManipulation TreeManipulation { get; set; }
        ScopeInfoExList ctrlScopeInfoExList = new ScopeInfoExList();
        AddScopeInfoEx ctrlAddScopeInfoEx = new AddScopeInfoEx();

        TableOfScopeInfo ctrlTableOfScopeInfo = new TableOfScopeInfo();
        TableOfScopeList ctrlTableOfScopeList = new TableOfScopeList();

        DependencyTablelList ctrlDependencyTablelList = new DependencyTablelList();
        AddDenpendencyTable ctrlAddDenpendencyTable = new AddDenpendencyTable();

        AddFileOfScope ctrlAddFileOfScope = new AddFileOfScope();
        ScopeFileList ctrlScopeFileList = new ScopeFileList();

        SummaryInfo ctrlSummaryInfo = new SummaryInfo();
        SyncHistoryList ctrlSyncHistoryList = new SyncHistoryList();
        ServerConfig ctrlServerConfig = new ServerConfig();
        ClientList ctrlClientList = null;
        public FrmMain()
        {
            InitializeComponent();
            FrmMain.MainForm = this;
            TreeManipulation = new AdvTreeManipulation(advTree1);
            ctrlClientList = new ClientList();
        }


        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                TreeManipulation.LoadData();
                TreeManipulation.BuildTree();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void advTree1_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelEx2.Controls.Clear();
            if (advTree1.SelectedIndex < 0)
            {
                AddControl(ctrlSummaryInfo);
                return;
            }
            Node current = advTree1.SelectedNode;
            if (current.Tag == null)
            {
                switch (current.Name)
                {
                    case AdvTreeManipulation.CommonScopeName:
                        ctrlScopeInfoExList.CurrentNode = advTree1.SelectedNode;
                        ctrlScopeInfoExList.DataBind(true);
                        AddControl(ctrlScopeInfoExList);
                        break;
                    case AdvTreeManipulation.DependencyTableName:
                        ctrlDependencyTablelList.CurrentNode = advTree1.SelectedNode;
                        ctrlDependencyTablelList.DataBind();
                        AddControl(ctrlDependencyTablelList);
                        break;
                    case AdvTreeManipulation.PrivateScopeName:
                        ctrlScopeInfoExList.CurrentNode = advTree1.SelectedNode;
                        ctrlScopeInfoExList.DataBind(false);
                        AddControl(ctrlScopeInfoExList);
                        break;
                    case AdvTreeManipulation.SyncScopeHistoryName:
                        ctrlSyncHistoryList.CurrentNode = advTree1.SelectedNode;
                        AddControl(ctrlSyncHistoryList);
                        break;
                    case AdvTreeManipulation.TableOfScopeName:
                        ctrlTableOfScopeList.CurrentNode = advTree1.SelectedNode;
                        ctrlTableOfScopeList.DataBind();
                        AddControl(ctrlTableOfScopeList);
                        break;
                    case AdvTreeManipulation.ClientListName:
                        AddControl(ctrlClientList);
                        break;
                    case AdvTreeManipulation.SummaryHistoryNodeName:
                        AddControl(ctrlSyncHistoryList);
                        break;
                    case AdvTreeManipulation.ServerVariableNodeName:
                        ctrlServerConfig.DataBind();
                        AddControl(ctrlServerConfig);
                        break;
                }


            }
            else
            {
                Type t = current.Tag.GetType();
                if (t.IsAssignableFrom(typeof(SyncConfigDS.ClientRow)))
                {


                }
                else
                    if (t.IsAssignableFrom(typeof(SyncConfigDS.DependencyTableRow)))
                    {

                        ctrlAddDenpendencyTable.BindingInfo(current);
                        AddControl(ctrlAddDenpendencyTable);

                    }
                    else
                        if (t.IsAssignableFrom(typeof(SyncConfigDS.ScopeInfoExRow)))
                        {
                            ctrlAddScopeInfoEx.BindingInfo(current);
                            AddControl(ctrlAddScopeInfoEx);

                        }
                        else
                            if (t.IsAssignableFrom(typeof(SyncConfigDS.TableOfScopeRow)))
                            {
                                ctrlTableOfScopeInfo.CurrentNode = current;
                                ctrlTableOfScopeInfo.DataBind();

                                AddControl(ctrlTableOfScopeInfo);

                            }


            }
            BuildNodePath(advTree1.SelectedNode);
        }
        void BuildNodePath(Node node)
        {
            Node temp = node;
            StringBuilder sb = new StringBuilder();

            while (temp != null)
            {
                sb.Insert(0, "\\" + temp.Text);
                temp = temp.Parent;
            }
            pTitle.Text = "  " + sb.ToString();
        }
        void AddControl(Control c)
        {
            c.Dock = DockStyle.Fill;
            c.BackColor = Color.Transparent;
            panelEx2.Controls.Clear();
            panelEx2.Controls.Add(c);
        }

        private void biReloadTree_Click(object sender, EventArgs e)
        {
            try
            {
                advTree1.SuspendLayout();
                advTree1.Nodes.Clear();
                TreeManipulation.LoadData();
                TreeManipulation.BuildTree();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                advTree1.ResumeLayout();
            }

        }

        private void panelEx2_ControlRemoved(object sender, ControlEventArgs e)
        {
            if (e.Control is UserControlBase)
            {
                ((UserControlBase)e.Control).Close();
            }
        }

        private void bbiExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void bbiInstallConfigScope_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(CommonVariable.SqlConnectionString);

                DbSyncScopeDescription scope = new DbSyncScopeDescription("ConfigScope");
                DbSyncTableDescription tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("ScopeInfoEx", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("TableOfScope", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("DependencyTable", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("ClientConfig", conn);
                scope.Tables.Add(tbl);
                //SqlSyncScopeDeprovisioning deprovi = new SqlSyncScopeDeprovisioning(conn);
                //deprovi.DeprovisionScope("ConfigScope");
                SqlSyncScopeProvisioning provi = new SqlSyncScopeProvisioning(conn);
                provi.ObjectSchema = "Sync";
                if (provi.ScopeExists(scope.ScopeName))
                {
                    MessageBox.Show("Đã tồn tại phạm vi cấu hình");
                    return;
                }
                else
                {
                    provi.PopulateFromScopeDescription(scope);
                    provi.Apply();
                    MessageBox.Show("Đã cài đặt thành công");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(CommonVariable.SqlConnectionString);

                DbSyncScopeDescription scope = new DbSyncScopeDescription("ConfigScope");
                DbSyncTableDescription tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("ScopeInfoEx", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("TableOfScope", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("DependencyTable", conn);
                scope.Tables.Add(tbl);
                tbl = SqlSyncDescriptionBuilder.GetDescriptionForTable("ClientConfig", conn);
                scope.Tables.Add(tbl);
                SqlSyncScopeDeprovisioning deprovi = new SqlSyncScopeDeprovisioning(conn);
                deprovi.ObjectSchema = "Sync";
                deprovi.DeprovisionScope("ConfigScope");
                //SqlSyncScopeProvisioning provi = new SqlSyncScopeProvisioning(conn);
                //provi.ObjectSchema = "Sync";
                //if (provi.ScopeExists(scope.ScopeName))
                //{
                //    MessageBox.Show("Đã tồn tại phạm vi cấu hình");
                //    return;
                //}
                //else
                //{
                //    provi.PopulateFromScopeDescription(scope);
                //    provi.Apply();
                //}
                MessageBox.Show("Đã xóa thành công");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAutoCreateScope_Click(object sender, EventArgs e)
        {
            ADOSyncConfig.CreateExtenScopeInfo();
        }

    }
}
